{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Copyright 2021 Huawei Technologies Co., Ltd\n",
    "\n",
    "CREATED:  2020-6-04 20:12:13\n",
    "MODIFIED: 2021-11-01 01:48:45\n",
    "\"\"\"\n",
    "\n",
    "# -*- coding:utf-8 -*-\n",
    "import cv2\n",
    "\n",
    "from acl_model import Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The autoreload extension is already loaded. To reload it, use:\n",
      "  %reload_ext autoreload\n"
     ]
    }
   ],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "device_id = 0\n",
    "model_path = \"model/facenet_tf.om\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ACL] init resource stage:\n",
      "[ACL] init resource stage success\n",
      "[Model] class Model init resource stage:\n",
      "[Model] create model output dataset:\n",
      "[Model] create model output dataset success\n",
      "model input size 1\n",
      "input  0\n",
      "model input dims ({'name': 'input', 'dimCount': 4, 'dims': [1, 160, 160, 3]}, 0)\n",
      "model input datatype 0\n",
      "==================================================\n",
      "model output size 1\n",
      "output  0\n",
      "model output dims ({'name': 'embeddings:0', 'dimCount': 2, 'dims': [1, 512]}, 0)\n",
      "model output datatype 0\n",
      "==================================================\n",
      "[Model] class Model init resource stage success\n",
      "[Model] class Model release source success\n",
      "[ACL] class Sample release source success\n"
     ]
    }
   ],
   "source": [
    "model = Model(device_id, model_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "img_path = \"data/angelina_jolie.jpg\"\n",
    "# load image\n",
    "img_bgr = cv2.imread(img_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[INFO] classifying face . . .\n",
      "[PreProc] img_resized shape (3, 160, 160)\n",
      "[ACL] img_host_ptr, img_buf_size:  94147025240208 307200\n",
      "[ACL] img_dev_ptr, img_buf_size:  18143024513024 307200\n",
      "[Model] create model input dataset:\n",
      "[Model] create model input dataset success\n",
      "[Model] execute stage:\n",
      "[Model] execute stage success\n",
      "[INFO] classification done!\n",
      "[Result] image runtime : 0.006\n"
     ]
    }
   ],
   "source": [
    "embeddings = model.run(img_bgr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4.48608398e-02, -3.31726074e-02, -5.89904785e-02,\n",
       "         7.01293945e-02, -3.29284668e-02, -1.62963867e-02,\n",
       "        -8.50219727e-02,  1.36871338e-02,  6.33239746e-03,\n",
       "        -1.54953003e-02,  2.01263428e-02, -1.30767822e-02,\n",
       "        -3.01971436e-02, -1.54724121e-02,  1.72729492e-02,\n",
       "        -4.09851074e-02,  6.92749023e-02,  8.64257812e-02,\n",
       "        -1.22299194e-02, -2.04010010e-02, -5.01403809e-02,\n",
       "         4.55627441e-02,  3.27453613e-02,  6.64520264e-03,\n",
       "        -7.16552734e-02,  4.38842773e-02, -2.72216797e-02,\n",
       "         2.49328613e-02,  6.17370605e-02,  6.34765625e-02,\n",
       "        -1.11328125e-01,  1.52492523e-03,  2.47039795e-02,\n",
       "        -6.26831055e-02, -1.53579712e-02,  3.18298340e-02,\n",
       "        -5.17883301e-02, -8.37707520e-03,  2.59704590e-02,\n",
       "         2.88238525e-02, -4.62646484e-02,  5.31387329e-03,\n",
       "        -4.49829102e-02, -7.36083984e-02,  1.56250000e-02,\n",
       "         2.74658203e-02,  1.31416321e-03,  2.20031738e-02,\n",
       "        -4.18701172e-02,  4.32739258e-02, -6.13403320e-03,\n",
       "         1.61437988e-02, -3.21388245e-03,  6.32934570e-02,\n",
       "        -7.33032227e-02,  1.45950317e-02,  4.52280045e-04,\n",
       "         6.60400391e-02,  6.75048828e-02, -1.89514160e-02,\n",
       "         7.21740723e-03,  3.82385254e-02, -3.81774902e-02,\n",
       "        -1.46031380e-04, -8.82720947e-03,  5.79223633e-02,\n",
       "         5.26123047e-02,  1.28402710e-02,  1.12056732e-03,\n",
       "         4.98352051e-02,  8.94165039e-03,  8.36791992e-02,\n",
       "         4.84466553e-03,  2.33306885e-02, -3.67431641e-02,\n",
       "        -7.98339844e-02, -1.06567383e-01, -9.13238525e-03,\n",
       "        -3.74755859e-02,  2.63214111e-02,  8.27026367e-02,\n",
       "         3.42102051e-02,  4.92858887e-02,  6.23779297e-02,\n",
       "         2.96173096e-02, -1.64508820e-05,  2.24456787e-02,\n",
       "         3.49731445e-02, -3.25622559e-02, -8.00609589e-04,\n",
       "         6.01196289e-02, -2.32849121e-02,  7.04956055e-02,\n",
       "        -2.03704834e-02,  4.61425781e-02,  6.74438477e-02,\n",
       "         2.51770020e-02,  3.81469727e-02, -6.30493164e-02,\n",
       "        -2.01416016e-02, -2.02369690e-03, -4.97436523e-02,\n",
       "        -2.87780762e-02,  4.33959961e-02,  9.69409943e-04,\n",
       "        -2.63061523e-02,  5.76477051e-02, -2.69317627e-02,\n",
       "        -3.26538086e-02, -2.65502930e-02,  3.28979492e-02,\n",
       "         1.06048584e-02, -6.40258789e-02, -6.31103516e-02,\n",
       "        -9.69696045e-03, -6.05468750e-02,  6.39038086e-02,\n",
       "        -3.45764160e-02, -1.45645142e-02,  5.63659668e-02,\n",
       "        -4.28466797e-02,  3.05557251e-03, -6.78100586e-02,\n",
       "        -5.19104004e-02, -1.94091797e-02, -4.45556641e-02,\n",
       "        -5.26428223e-03,  6.02722168e-03, -7.52563477e-02,\n",
       "        -3.49731445e-02, -4.18090820e-02, -2.63214111e-02,\n",
       "        -1.14135742e-02, -3.42712402e-02, -8.79516602e-02,\n",
       "        -8.88824463e-03, -4.72106934e-02,  2.59399414e-02,\n",
       "         7.22656250e-02,  1.23229980e-01, -8.36944580e-03,\n",
       "         6.51855469e-02,  1.48010254e-02,  5.58166504e-02,\n",
       "         5.33447266e-02, -2.63824463e-02, -3.08685303e-02,\n",
       "        -2.54669189e-02, -5.69152832e-02,  2.02026367e-02,\n",
       "         8.11157227e-02, -4.49371338e-03, -3.32946777e-02,\n",
       "         4.21142578e-02,  4.27856445e-02, -3.79028320e-02,\n",
       "        -2.74810791e-02, -3.95202637e-03,  1.16157532e-03,\n",
       "        -7.29370117e-02,  6.83593750e-02, -3.12423706e-03,\n",
       "        -3.28063965e-02,  5.34973145e-02,  2.09350586e-02,\n",
       "         7.89642334e-03,  8.88347626e-04, -5.41687012e-02,\n",
       "        -2.76489258e-02,  5.03540039e-02, -6.13403320e-02,\n",
       "         1.05438232e-02, -2.05688477e-02, -5.12084961e-02,\n",
       "         1.81274414e-02,  5.66406250e-02,  3.41796875e-02,\n",
       "         1.75189972e-03,  1.70745850e-02, -5.40771484e-02,\n",
       "        -2.79388428e-02, -1.06018066e-01, -6.89697266e-02,\n",
       "         5.69152832e-02,  2.61688232e-02,  5.87158203e-02,\n",
       "        -3.38745117e-03,  2.15454102e-02,  5.11932373e-03,\n",
       "         3.70178223e-02,  5.73730469e-02, -3.05633545e-02,\n",
       "         4.65698242e-02, -1.03225708e-02,  4.99877930e-02,\n",
       "        -3.96728516e-02, -5.87158203e-02,  8.97216797e-03,\n",
       "        -5.68847656e-02,  5.42297363e-02,  1.33895874e-02,\n",
       "         8.77685547e-02, -4.69665527e-02, -6.83212280e-03,\n",
       "         6.36672974e-03,  2.82440186e-02, -6.82353973e-04,\n",
       "        -3.08380127e-02,  1.62658691e-02,  5.25207520e-02,\n",
       "         3.12500000e-02, -1.20544434e-02, -4.22973633e-02,\n",
       "         1.10397339e-02, -4.26635742e-02, -6.11782074e-04,\n",
       "        -5.19714355e-02,  7.42187500e-02, -5.83801270e-02,\n",
       "         6.03027344e-02, -1.37252808e-02, -5.97534180e-02,\n",
       "        -9.68017578e-02, -6.24084473e-02, -3.31115723e-02,\n",
       "        -1.33819580e-02, -3.08532715e-02,  5.51452637e-02,\n",
       "        -4.69970703e-02,  1.04217529e-02,  7.61108398e-02,\n",
       "        -2.66113281e-02, -1.08261108e-02, -1.06201172e-01,\n",
       "        -3.57360840e-02, -1.78070068e-02,  2.18963623e-02,\n",
       "         2.56042480e-02, -2.47192383e-02, -1.79748535e-02,\n",
       "         4.06494141e-02,  1.34811401e-02,  2.55279541e-02,\n",
       "         2.95104980e-02,  1.04141235e-02,  6.04553223e-02,\n",
       "        -2.70385742e-02,  6.27441406e-02,  3.55339050e-03,\n",
       "         9.82666016e-03, -1.77307129e-02,  2.78854370e-03,\n",
       "        -1.34277344e-02,  3.62548828e-02,  1.54266357e-02,\n",
       "         3.52172852e-02,  2.30712891e-02, -1.13296509e-02,\n",
       "         6.42089844e-02,  6.67724609e-02, -1.57165527e-02,\n",
       "        -6.39648438e-02,  5.49926758e-02, -6.47583008e-02,\n",
       "        -1.65863037e-02, -1.91345215e-02,  5.74951172e-02,\n",
       "         4.11682129e-02, -5.04760742e-02, -4.02221680e-02,\n",
       "        -5.56564331e-03, -2.02178955e-02,  7.97119141e-02,\n",
       "         4.78820801e-02,  3.64074707e-02, -8.36181641e-03,\n",
       "        -1.48010254e-02,  8.96453857e-03, -9.41467285e-03,\n",
       "        -5.60302734e-02, -3.11088562e-03,  2.39105225e-02,\n",
       "        -4.54101562e-02, -6.16149902e-02, -3.90319824e-02,\n",
       "         3.83377075e-03,  2.29797363e-02,  8.14208984e-02,\n",
       "        -5.41076660e-02, -8.05664062e-02, -1.76239014e-02,\n",
       "        -7.81250000e-02,  2.99835205e-02, -1.94702148e-02,\n",
       "        -7.67822266e-02, -1.16516113e-01,  1.95922852e-02,\n",
       "         3.54309082e-02,  1.67083740e-02, -3.37829590e-02,\n",
       "         6.44531250e-02, -7.67822266e-02,  5.25817871e-02,\n",
       "         2.27966309e-02,  1.45034790e-02, -3.64074707e-02,\n",
       "         1.81121826e-02, -5.28259277e-02,  3.21350098e-02,\n",
       "         3.21350098e-02,  2.63214111e-02,  3.61022949e-02,\n",
       "         2.63366699e-02,  2.36053467e-02,  3.71093750e-02,\n",
       "        -2.93922424e-03, -8.88671875e-02,  2.63214111e-02,\n",
       "         1.77764893e-02,  3.39965820e-02,  1.63269043e-02,\n",
       "         1.22375488e-02,  3.06396484e-02,  6.65893555e-02,\n",
       "        -3.80859375e-02, -4.59594727e-02, -7.80105591e-03,\n",
       "         9.44519043e-03,  4.51354980e-02,  3.71704102e-02,\n",
       "         3.57666016e-02, -4.69055176e-02, -2.75421143e-03,\n",
       "         3.27453613e-02,  1.96075439e-02, -2.34413147e-03,\n",
       "        -4.67529297e-02, -5.35888672e-02,  2.54669189e-02,\n",
       "        -2.92358398e-02,  9.10034180e-02,  3.37524414e-02,\n",
       "         1.23405457e-03, -6.59179688e-02,  3.76892090e-02,\n",
       "        -3.93676758e-02, -4.69665527e-02,  3.30200195e-02,\n",
       "         8.81347656e-02,  2.72369385e-02,  4.56542969e-02,\n",
       "        -2.85491943e-02, -8.89282227e-02,  1.28326416e-02,\n",
       "        -6.79931641e-02,  2.51617432e-02,  4.31213379e-02,\n",
       "         2.06756592e-02,  2.40325928e-03,  3.03649902e-02,\n",
       "        -7.72705078e-02,  4.24804688e-02, -2.09960938e-02,\n",
       "         4.14733887e-02, -9.27734375e-03, -2.44903564e-02,\n",
       "         1.06384277e-01, -1.15203857e-02, -3.96423340e-02,\n",
       "        -4.87365723e-02, -5.97534180e-02, -1.95770264e-02,\n",
       "        -2.36816406e-02, -9.74273682e-03,  6.98242188e-02,\n",
       "         1.01379395e-01, -1.20620728e-02,  2.52227783e-02,\n",
       "        -8.09326172e-02, -1.72271729e-02,  1.79748535e-02,\n",
       "        -5.07202148e-02, -5.09033203e-02,  9.74731445e-02,\n",
       "         8.56933594e-02, -4.19311523e-02, -4.64782715e-02,\n",
       "        -1.05056763e-02, -1.40991211e-02,  1.56707764e-02,\n",
       "         2.91748047e-02,  5.50842285e-02, -2.35748291e-02,\n",
       "        -8.01391602e-02, -5.58471680e-02,  2.38647461e-02,\n",
       "         5.73120117e-02, -2.41851807e-02,  5.51986694e-03,\n",
       "         2.63519287e-02, -4.08020020e-02,  2.60467529e-02,\n",
       "        -3.05175781e-02,  6.65283203e-02,  2.20642090e-02,\n",
       "        -3.69873047e-02, -4.14428711e-02, -5.95092773e-02,\n",
       "        -2.13470459e-02, -4.62036133e-02,  3.72009277e-02,\n",
       "        -3.42407227e-02,  4.15344238e-02,  3.08380127e-02,\n",
       "         2.84314156e-05,  1.15299225e-03,  8.03222656e-02,\n",
       "         1.02478027e-01, -4.01916504e-02, -4.21447754e-02,\n",
       "         1.78375244e-02,  2.44522095e-03,  1.24511719e-02,\n",
       "         1.87110901e-03, -7.00683594e-02,  6.80160522e-03,\n",
       "         6.78100586e-02, -6.29425049e-03, -6.10961914e-02,\n",
       "        -5.37109375e-02,  4.93164062e-02,  3.46069336e-02,\n",
       "        -2.61230469e-02, -2.35137939e-02, -5.53588867e-02,\n",
       "         4.80651855e-02, -7.55691528e-03, -1.53808594e-02,\n",
       "         4.15344238e-02,  1.19171143e-02, -1.70135498e-02,\n",
       "         5.33447266e-02, -4.91638184e-02,  6.54296875e-02,\n",
       "        -4.50134277e-02,  5.65795898e-02, -2.59704590e-02,\n",
       "         5.28259277e-02, -6.02111816e-02, -3.95812988e-02,\n",
       "        -7.43484497e-03,  5.67016602e-02,  5.49926758e-02,\n",
       "        -1.24816895e-02,  3.87573242e-02, -1.69525146e-02,\n",
       "         3.40576172e-02,  4.30297852e-02,  5.33447266e-02,\n",
       "         5.28869629e-02, -5.34973145e-02, -3.16162109e-02,\n",
       "        -2.79998779e-02, -2.38037109e-03,  8.21533203e-02,\n",
       "         1.06430054e-02, -1.78985596e-02, -3.09143066e-02,\n",
       "         3.59497070e-02,  1.93176270e-02,  1.34429932e-02,\n",
       "        -2.84423828e-02,  8.59985352e-02, -4.77905273e-02,\n",
       "        -1.29928589e-02,  6.24084473e-03,  5.97229004e-02,\n",
       "        -7.28149414e-02,  4.22973633e-02,  6.53076172e-02,\n",
       "         9.39331055e-02,  3.22875977e-02,  1.69525146e-02,\n",
       "         1.34468079e-03, -6.42013550e-03,  6.44302368e-03,\n",
       "        -9.13085938e-02,  5.49926758e-02,  5.41687012e-02,\n",
       "        -2.55889893e-02,  4.17709351e-03, -1.15814209e-02,\n",
       "        -2.13775635e-02, -2.32696533e-02,  6.93969727e-02,\n",
       "        -3.86047363e-02,  3.39050293e-02,  1.95312500e-02,\n",
       "        -1.38168335e-02, -6.75659180e-02, -3.97338867e-02,\n",
       "         4.73937988e-02,  2.62641907e-03,  2.85186768e-02,\n",
       "         2.83813477e-02,  4.11071777e-02, -1.52816772e-02,\n",
       "         1.77001953e-02, -5.19752502e-04, -1.00708008e-02,\n",
       "         4.40063477e-02, -2.71148682e-02]], dtype=float32)"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# show embeddings\n",
    "embeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
